package com.party.core.service.crowdfund.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.core.dao.read.crowdfund.EventGradationReadDao;
import com.party.core.dao.write.crowdfund.EventGradationWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.city.Area;
import com.party.core.model.city.City;
import com.party.core.model.crowdfund.EventGradation;
import com.party.core.service.city.IAreaService;
import com.party.core.service.city.ICityService;
import com.party.core.service.crowdfund.IEventGradationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @ClassName CrowdfundEventGradationService
 * @Description 主题阶段信息接口
 * @Author yifeng
 * @Date 2019/3/20 9:32
 **/
@Service
public class EventGradationService implements IEventGradationService {
    @Autowired
    private EventGradationReadDao eventGradationReadDao;
    @Autowired
    private EventGradationWriteDao eventGradationWriteDao;
    @Autowired
    private ICityService cityService;
    @Autowired
    private IAreaService areaService;

    @Override
    public String insert(EventGradation eventGradation) {
        BaseModel.preInsert(eventGradation);
        boolean result = eventGradationWriteDao.insert(eventGradation);
        if (result) {
            return eventGradation.getId();
        }
        return null;
    }

    @Override
    public boolean update(EventGradation eventGradation) {
        if (null != eventGradation.getUpdateDate()) {
            eventGradation.setUpdateDate(new Date());
        }
        return eventGradationWriteDao.update(eventGradation);
    }

    @Override
    public boolean deleteLogic(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return eventGradationWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return eventGradationWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return eventGradationWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return eventGradationWriteDao.batchDelete(ids);
    }

    @Override
    public EventGradation get(String id) {
        return eventGradationReadDao.get(id);
    }

    @Override
    public List<EventGradation> listPage(EventGradation eventGradation, Page page) {
        return eventGradationReadDao.listPage(eventGradation, page);
    }

    @Override
    public List<EventGradation> list(EventGradation eventGradation) {
        return eventGradationReadDao.listPage(eventGradation, null);
    }

    @Override
    public List<EventGradation> batchList(Set<String> ids, EventGradation eventGradation, Page page) {
        return null;
    }


    @Override
    public List<EventGradation> findByEventId(String eventId) {
        EventGradation gradation = new EventGradation();
        gradation.setEventId(eventId);
        return eventGradationReadDao.listPage(gradation, null);
    }

    /**
     * @Description: 获取正在进行的阶段主题
     * @Param: eventId
     * @return: EventGradation
     * @Author: Hua Qunhao
     * @Date: 2019-03-21-0021
     */
    @Override
    public EventGradation findOngoingGradation(String eventId) {
        return eventGradationReadDao.findOngoingGradation(eventId);
    }


    /***
     * 根据projectId 找到当前众筹者正在众筹哪个阶段
     * @param projectId
     * @return
     */
    @Override
    public EventGradation getOngoingGradation(String projectId) {
        return eventGradationReadDao.getOngoingGradation(projectId);
    }


    @Override
    public List<EventGradation> withCityNameByEventId(String eventId) {
        return eventGradationReadDao.withCityNameByEventId(eventId);
    }

    /**
     * @Description:  获取阶段众筹主题金额
     * @Param: eventGradation，sign（0：众筹结束阶段/1：报名阶段和未开始的阶段）
     * @return:
     * @Author: Hua Qunhao
     * @Date: 2019-03-22-0022
     */
    @Override
    public Double getGradationTotalAmount(EventGradation eventGradation, Integer sign) {
        return eventGradationReadDao.getGradationTotalAmount(eventGradation,sign);
    }

    /**
     * @Description:  获取阶段众筹主题列表
     * @Param: eventGradation，sign（0：众筹结束阶段/1：正在进行的和未开始的阶段）
     * @return:
     * @Author: Hua Qunhao
     * @Date: 2019-03-22-0022
     */
    @Override
    public List<EventGradation> getGradationList(EventGradation eventGradation, Integer sign) {
        return eventGradationReadDao.getGradationList(eventGradation, sign);
    }



    /***
     * 根据阶段数找到阶段主题
     * @param eventGradation
     * @return
     */
    @Override
    public EventGradation getTheNextGradation(EventGradation eventGradation) {
        return eventGradationReadDao.getTheNextGradation(eventGradation);
    }

    /**
     * @param eventGradation
     * @return com.party.core.model.crowdfund.EventGradation
     * @Author yifeng
     * @Description 获取下个阶段
     * @Date 17:48 2019/3/22
     **/
    @Override
    public List<EventGradation> getNextGradations(EventGradation eventGradation) {
        return eventGradationReadDao.getNextGradations(eventGradation);
    }

    /***
     * 根据当前阶段获取之后的所有阶段
     * @param eventGradation
     * @return
     */
    @Override
    public List<EventGradation> getAfterEventGradations(EventGradation eventGradation) {
        return eventGradationReadDao.getAfterEventGradations(eventGradation);
    }

    /**
     * @Description: 获取主题的最后的阶段
     * @Param:  eventId
     * @return: EventGradation
     * @Author: Hua Qunhao
     * @Date: 2019-03-27-0027
     */
    @Override
    public EventGradation getTheLastGradation(String eventId) {
        return eventGradationReadDao.getTheLastGradation(eventId);
    }

    /**
     * @Description: 获取阶段地址
     * @Param: [eventGradation]
     * @return: java.lang.String
     * @Author: Hua Qunhao
     * @Date: 2019-03-30-0030
     */
    @Override
    public String getShowAddress(EventGradation eventGradation) {
        City c = cityService.get(eventGradation.getCityId());
        String address = c != null ? c.getName() : "";
        Area area = areaService.get(eventGradation.getArea());
        if (area != null) {
            address += area.getName();
        } else {
            address += eventGradation.getArea() != null ? eventGradation.getArea() : "";
        }
        address += eventGradation.getPlace() != null ? eventGradation.getPlace() : "";
        return address;
    }


    /** 
    * @Description:获取阶段地址
    * @Param: [activity] 
    * @return: java.util.Map<java.lang.String,java.lang.String> 
    * @Author: Hua Qunhao
    * @Date: 2019-03-30-0030 
    */
    @Override
    public String getShowAddressMap(EventGradation eventGradation, Map<String,String> mmp) {
        City c = cityService.get(eventGradation.getCityId());
        String address = c != null ? c.getName() : "";
        mmp.put("city",c != null ? c.getName() : "");
        Area area = areaService.get(eventGradation.getArea());
        if (area != null) {
            mmp.put("area",area.getName()) ;
            address += area.getName();
        } else {
            mmp.put("area",eventGradation.getArea() != null ? eventGradation.getArea() : "") ;
            address += eventGradation.getArea() != null ? eventGradation.getArea() : "";
        }
        mmp.put("place",eventGradation.getPlace() != null ? eventGradation.getPlace() : "");
        address += eventGradation.getPlace() != null ? eventGradation.getPlace() : "";
        return address;
    }
}
